/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2020-2022. All rights reserved.
 * Description: 
 * Author: g00421808
 * Create: 12/26/2022
 * Notes: 
 */

#ifndef JSBIND_JS_FUNCTION_H
#define JSBIND_JS_FUNCTION_H

#include "jsbind/logging/logging.h"
#include "jsbind/callback/napi/safety_callback.h"

namespace Jsb {
class JSFunction {
public:
    JSFunction(napi_env env, napi_value cb)
        : ctx_(std::make_shared<ThreadSafeContext>(env, cb))
    { }

    template<typename R, typename... P>
    R Invoke (P... args) const {
        ctx_->forwardCallJs = SafetyCallback<R (P...)>::CallJs;
        return(ctx_->template Invoke<R, P...>(std::move(args)...));
    }

private:
    std::shared_ptr<ThreadSafeContext> ctx_;
};
} // namespace Jsb
#endif //JSBIND_JS_FUNCTION_H
